package com.wl.rjwmc.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.rjwmc.Dao.CategoryDao;
import com.wl.rjwmc.DoMain.Category;
import com.wl.rjwmc.DoMain.Dish;
import com.wl.rjwmc.DoMain.Setmeal;
import com.wl.rjwmc.Service.CategoryService;
import com.wl.rjwmc.Service.DishService;
import com.wl.rjwmc.Service.SetmealService;
import com.wl.rjwmc.Utools.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> lqwDish = new LambdaQueryWrapper<>();
        lqwDish.eq(Dish::getCategoryId,id);
//        分类下关联菜品的数量
        int count1 = dishService.count(lqwDish);
        if(count1>0){
//            分类下有关联菜品
            throw new CustomException("菜品下关联了商品");
        }
        LambdaQueryWrapper<Setmeal> lqwSetmeal = new LambdaQueryWrapper<>();
        lqwSetmeal.eq(Setmeal::getCategoryId,id);
//        套餐内关联菜品数量
        int count2 = setmealService.count(lqwSetmeal);
        if(count2>0){
//            分类下有关联菜品
            throw new CustomException("套餐下关联了商品");
        }
        super.removeById(id);
    }
}
